<?php
/**
 * Created by PhpStorm.
 * User: WX-C
 * Date: 2018/4/19
 * Time: 19:12
 */

namespace app\common\model;


use think\Model;
use think\Request;
use think\View;

class Answer extends Model
{
    protected $autoWriteTimestamp='int';
    protected $createTime='created_time';
    protected $updateTime=false;
    protected $insert=['ip'];
    protected $readonly=['naire','long_json','ip','created_time'];

    public function __construct($data = [])
    {
        $this->event('after_write',function($row){
            $id=$row['naire'];
            Questionnaire::where(['id'=>$id])
                ->setInc('count_people',1);
        });
        parent::__construct($data);
    }


    protected function setIpAttr(){
        $ip=Request::instance()->ip();
        return ip2long($ip);
    }
    protected function getIpAttr($value){
        return long2ip($value);
    }
    protected function setLongJsonAttr($value){
        return json_encode($value,JSON_UNESCAPED_UNICODE);
    }
    protected function getLongJsonAttr($value){
        return json_decode($value,true);
    }
    public function add($data){
        return $this->isUpdate(false)
            ->allowField(true)
            ->save($data);
    }

    /**
     * 在接受回答完成后的清洗
     */
    public function acceptAnswer($data){
        $id=$data['id'];
        unset($data['__token__']);
        $mm=$data;
        foreach ( $mm as $key=>$value){
            if(strpos($key,'_')!==false){
                list($name,$_)=explode('_',$key);
                list($a,$b)=explode('|',$value);
                $data[$name][$a]=$b;
            }
        }
//        print_r($data);exit;
        $naire=Questionnaire::where(['id'=>$id])
            ->find();
        $questions=Question::where(['id'=>['in',$naire->qids]])
            ->select();
        $tmp=[];
        foreach ($questions as $question){
            $id2md5=md5($question->id);
            $answer=$data[$id2md5];
            switch ($question['type']){
                case 'radio':
                    $tmp[]=['id'=>$question->id,'name'=>$answer];
                    break;
                case 'checkbox':
                    $tmp[]=['id'=>$question->id,'name'=>$answer];
                    break;
                case 'text':
                case 'textarea':
                    $value=htmlspecialchars($answer);
                    $tmp[]=['id'=>$question->id,'name'=>$value];
                    break;
                case 'table':
                    $tmp[]=['id'=>$question->id,'name'=>$answer];
            }
        }
        return $tmp;
    }


    public function index(){
        $request=Request::instance();
        $limit=$request->param('limit',10,'intval');
        $page=$request->param('page',0,'intval');
        $name=$request->param('name','');
        $where=[];
        if($name){
            $where=['name'=>['like',"%{$name}%"]];
        }

        $count=$this
            ->where($where)
            ->count('id');

        $lists=$this
            ->with('questionnaire')
            ->page($page,$limit)
            ->where($where)
            ->select();
        return [$lists,$count];
    }
    public function question(){
        $qids=array_column($this->long_json,'id');
        $data=Question::where(['id'=>['in',$qids]])
            ->column('','id');
        return $data;
    }
    public function questionnaire(){
        return $this->belongsTo('Questionnaire','naire','id');
    }
    public function detail($id){
        $row=$this->get($id);
        View::share('row',$row);
        $questions=$row->questionnaire->question();
        $html='';
        $form=new Form();
        foreach ($row->long_json as $index=>$l){
            $q=$questions[$l['id']];
            $q['ext']=json_decode($q['ext'],true);
            switch ($q['type']){
                case 'radio':
                    $html.=$form->radio($index,$q['name'],'',$q['ext'],$l['value']);
                    break;
                case 'checkbox':
                    $html.=$form->checkbox($index,$q['name'],'',$q['ext'],$l['value']);
                    break;
                case 'textarea':
                    $html.=$form->textarea($index,$q['name'],'',$l['value']);
                    break;
                case 'table':
                    $html.=$form->table($index,$q['name'],'',$q['ext'],$l['value']);
                    break;
                case 'text':
                    $html.=$form->text($index,$q['name'],'',$l['value']);
                    break;
            }
        }
        return $html;
    }
}